<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">

            <!-- 放置表单 -->
            <el-form ref="userForm" :model="userInfo" :rules="rules" label-width="120px" style="margin-left: 120px; margin-top:30px">
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="userInfo.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="新密码:" prop="password">
                <el-input v-model="userInfo.password" style="width:300px" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="saveUser">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <!-- 放置个人详情 -->
            <el-row type="flex" justify="end">
              <el-tooltip content="打印个人基本信息">
                <router-link :to="`/employees/print/${userId}?type=personal`">
                  <i class="el-icon-printer" />
                </router-link>
              </el-tooltip>
            </el-row>
            <component :is="userComponent" />
            <!-- <user-info /> -->
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <el-row type="flex" justify="end">
              <el-tooltip content="打印岗位信息">
                <router-link :to="`/employees/print/${userId}?type=job`">
                  <i class="el-icon-printer" />
                </router-link>
              </el-tooltip>
            </el-row>
            <!-- 动态组件 -->
            <component :is="JobInfo" />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
// 获取用户信息
import { getUserDetailById } from '@/api/user'
// 保存 用户信息
import { saveUserDetailById } from '@/api/employees'
// 引入 个人详情组建
import userComponent from './components/user-info.vue'
// 引入公司信息
import JobInfo from './components/job-info.vue'
export default {
  components: {
    userComponent,
    JobInfo
  },
  data() {
    return {
      // 个人详情
      userComponent: 'userComponent',
      // 公司详情
      JobInfo: 'JobInfo',
      // 这样可以后面直接通过 this.userId进行获取数据
      userId: this.$route.params.id,
      // 存放基本信息
      userInfo: {
        username: '',
        password: ''
      },
      // 校验规则
      rules: {
        username: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 15, message: '密码长度6-15位', trigger: 'blur' }]
      }
    }
  },
  created() {
    // 初始化数据  页面一开始刷新就调用
    this.getUserDetailById()
  },
  methods: {
    // 获取用户基本信息
    async getUserDetailById() {
      // 数据传值 接收
      this.userInfo = await getUserDetailById(this.userId)
      // console.log(this.userinfo)
    },
    async   saveUser() {
      try {
        // 自定义校验表单
        await this.$refs.userForm.validate()
        // 发送请求  更新数据
        // 将新密码的值替换原密码的值
        await saveUserDetailById({ ...this.userInfo, password: this.userInfo.password })
        // 更新成功
        this.$message.success('保存成功')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style>

</style>
